
// hash - 时间戳
function pz(val, len = 2) {
  val = val + ''
  if (!val) {
    return val
  }
  if (String(val).length >= len) {
    return val
  }
  return `${new Array(len - 1).fill('0').join('')}${val}`
}
const _t = new Date()
export const _hash = '' + _t.getFullYear() + pz(_t.getMonth() + 1) + pz(_t.getDate()) + pz(_t.getHours()) + pz(_t.getMinutes()) + pz(_t.getSeconds()) + pz(_t.getMilliseconds(), 3)

// inject script
export function injectScript () {
  return `<script>console.log('inject script')</script>`
}

// wmda - 埋点
export function injectWMDA (extendConfig) {
  // const domain = window?.document?.domain || location?.host || ''
  const WMDA_SDK_CONFIG = {
    api_v: 1,
    sdk_v: 0.1,
    mode: 'report',
    // cookie_domain: domain,
    cate_id: '',
    page_extra: [
      {
        key: '',
        value: '',
      },
    ],
    user_extra: [
      {
        key: '',
        value: '',
      },
    ],
    channel: '',   //按需填写
    getUserId: function() {},   //按需填写
    getDeviceId: function() {},   //按需填写
    SPA_HASH: true, //单页应用必选
    ...extendConfig,
  }
  const _tempStr = `
  <script type="text/javascript">
    window.WMDA_SDK_CONFIG = (${JSON.stringify(WMDA_SDK_CONFIG)});
  </script>
  <script type="text/javascript" src="http://j1.58cdn.com.cn/wmda/js/statistic.js"></script>
  `
  return _tempStr
}


